/*
	Azid Engine is 3D Game Engine developed along side with Galactic Siege (see Google Code) 
	Copyright (C) 2010  Jorgen Ader

	This file is part of AzidEngine.

	AzidEngine is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	AzidEngine is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with AzidEngine.  If not, see <http://www.gnu.org/licenses/>. 
*/

#include "../includes/EngineObjectManager.hpp"

using namespace Ogre;
using namespace AzidEngine;

ObjectManager::ObjectManager()
{
	sceneMgr = AzidGame::getSingletonPtr()->mSceneMgr;
}

ObjectManager::~ObjectManager()
{
	EntityDataContainerId id;
	for( id = entityData.begin(); id < entityData.end(); id++)
	{
		if( *id != 0)
			delete *id;
	}
	entityData.clear();
}

void ObjectManager::addEntity(EntityData* data)
{
	entityData.push_back(data);
}

void ObjectManager::removeEntity(EntityData *data)
{
	EntityDataContainerId id;
	for( id = entityData.begin(); id < entityData.end(); id++)
		if((*id) == data)
		{
			if(sceneMgr->hasSceneNode(data->nodeName))
			{
				SceneNode* node = sceneMgr->getSceneNode(data->nodeName);
				node->detachObject(data->entityName);
				sceneMgr->destroyEntity(data->entity);
			}
			entityData.erase(id);
		}
}

EntityDataContainer ObjectManager::getEntityList(String arch, String child)
{
	if(arch == "")
		return entityData;
	EntityDataContainer t;
	EntityDataContainerId it;
	for(it = entityData.begin(); it < entityData.end(); it++)
	{
		if( (*it)->archType == arch && ((*it)->childType == child || child == ""))
			t.push_back(*it);
	}
	return t;
}

EntityData* ObjectManager::getEntityByName(String name)
{
	EntityDataContainerId it;
	for(it = entityData.begin(); it < entityData.end(); it++)
	{
		if((*it)->entityName == name)
			return (*it);
	}
	return NULL;
}

EntityData* ObjectManager::getEntityByPtr(Entity* ent)
{
	EntityDataContainerId it;
	for(it = entityData.begin(); it < entityData.end(); it++)
	{
		if((*it)->entity == ent)
			return (*it);
	}
	return NULL;
}
